﻿using de.wolfmarco.pas.Shared.Language;
using Microsoft.Practices.Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;

namespace de.wolfmarco.pas.Shared.Helper
{
    public static class BindingHelper
    {
        public static void BindElementToModule(FrameworkElement element, ModuleInfo moduleInfo)
        {
            element.Name = moduleInfo.ModuleName + "Button";
            element.ToolTip = ModuleDisplayNames.GetModuleDisplayName(moduleInfo);

            BindElement(element, "StartModuleCommand", moduleInfo);
        }

        public static void BindElement(FrameworkElement element, string command, object source)
        {
            BindElementToCommand(element, command);
            BindElementToSource(element, source);
        }

        public static void BindElementToCommand(FrameworkElement element, string command)
        {
            DependencyProperty commandProperty;

            if (element is ButtonBase)
            {
                commandProperty = ButtonBase.CommandProperty;
            }
            else if (element is MenuItem)
            {
                commandProperty = MenuItem.CommandProperty;
            }
            else
            {
                throw new NotSupportedException(element.GetType().Name + " is not supported");
            }

            element.SetBinding(commandProperty, CreateBinding(command));
        }

        public static void BindElementToSource(FrameworkElement element, object source)
        {
            DependencyProperty commandParameterProperty;

            if (element is ButtonBase)
            {
                commandParameterProperty = ButtonBase.CommandParameterProperty;
            }
            else if (element is MenuItem)
            {
                commandParameterProperty = MenuItem.CommandParameterProperty;
            }
            else
            {
                throw new NotSupportedException(element.GetType().Name + " is not supported");
            }

            element.SetBinding(commandParameterProperty, new Binding() { Source = source });
        }

        public static Binding CreateBinding(string path)
        {
            Binding binding = new Binding();
            binding.Path = new PropertyPath(path);
            return binding;
        }

        public static void BindElementToDefaultProperty(FrameworkElement element, string path)
        {
            Binding binding = CreateBinding(path);
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            element.SetBinding(GetDefaultProperty(element), binding);
        }

        public static DependencyProperty GetDefaultProperty(FrameworkElement element)
        {
            if (element is TextBox)
                return TextBox.TextProperty;
            else if (element is ComboBox)
                return ComboBox.SelectedValueProperty;
            else if (element is DatePicker)
                return DatePicker.SelectedDateProperty;

            throw new Exception(element + " is a unknown FrameworkElement");
        }
    }
}
